package com.cskaoyan.servlet;

import com.cskaoyan.bean.CommentParameter;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.common.CommonServlet;
import com.cskaoyan.model.MarketComment;
import com.cskaoyan.service.CommentService;
import com.cskaoyan.service.impl.CommentServiceImpl;

import com.cskaoyan.utils.JacksonUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/admin/comment/*")
public class AdminCommentServlet extends CommonServlet {
    CommentService commentService = new CommentServiceImpl();

    //评论列表
    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        //获取参数
        CommentParameter commentParameter = new CommentParameter();
        //固定的形式
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 使用把第二个参数的属性赋值给第一个参数
        BeanUtils.copyProperties(commentParameter,parameterMap);

        //做list业务
        BaseRespVo baseRespVo = commentService.list(commentParameter);

        //返回响应
        return baseRespVo;

    }


    //评论删除
    public BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, IOException {
        //获取参数,此时为Json语句
        String jsonStr = request.getReader().readLine();
        //将jsonStr转化为对象
        MarketComment marketComment = JacksonUtil.read(jsonStr, MarketComment.class);
        //做逻辑delete业务
        BaseRespVo baseRespVo = commentService.delete(marketComment);
        //返回响应
        return baseRespVo;
    }
}
